package com.avocado.base.repository;

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.avocado.persistence.repository.BaseRepository;
import com.avocado.base.entity.User;
import org.springframework.data.repository.query.Param;

public interface UserRepository extends BaseRepository<User, Long> {
	
	@Query("select count(1) from User where name=:name")
	Long getCountByName(@Param("name")String name);
	
	
	@Query("from User where name=:name and  deleted=false")
	List<User> findAllByName(@Param("name")String name);


	@Query("select t.user from UserRole t where t.role.name=:name and t.user.deleted=false")
	List<User> findAllByRoleName(@Param("name")String name);

	@Query("select t.user from UserRole t where t.role.code=:code and t.user.deleted=false")
	List<User> findAllByRoleCode(@Param("code")String code);

	@Modifying
	@Query("update  User set password=?1  where id=?2")
	Integer updatePassword(String password,Long userId);

	@Query("from User where name<>:exceptName and deleted=false order by name asc")
	List<User> findAllExceptName(@Param("exceptName")String exceptName);

	@Query("from User where name<>:exceptName and realName LIKE CONCAT(:likeName,'%') and deleted=false order by name asc")
	List<User> findAllExceptName(@Param("exceptName")String exceptName, @Param("likeName")String likeName);


	@Query("from User where organization.id=:departmentId and deleted=false")
	List<User> findAllByDepartmentId(@Param("departmentId")Long  departmentId);

	@Modifying
	@Query("update  User set photo=:photo  where name=:userName and deleted=false ")
	Integer updatePhoto(@Param("photo")String photo,@Param("userName")String  userName);


	@Query("from User where wechatId=:wechatId and deleted=false")
	List<User> findAllByWechatId(@Param("wechatId")String   wechatId);

}
